<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('security');
		$this->load->library('tank_auth');
	}

	function _remap($method, $params = array())
	{
		
		if (!$this->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		} elseif(!$this->tank_auth->is_profile_set()) {
			redirect('/auth/set_profile/');
		} else {
			$this->profiles=$this->tank_auth->get_profile();
			$isOwner=false;
			foreach($this->profiles as $row){
				if($row->type==="Owner"){
					$isOwner=true;
				}
			}
			if ($isOwner){
				if (method_exists($this, $method))
				{
					return call_user_func_array(array($this, $method), $params);
				}
			}
		}
		//show_404(); // Either means that the user isn't authorized or that the page doesn't exist.
	}

	function index()
	{
		$this->load->model('clientmodel');
		$this->load->model('invoicemodel');
		$data['head']       = $this->load->view('backend/invoices/form_head',null,true);
		$data['head']      .= $this->load->view('backend/invoices/table_head',null,true);
		$data['head']      .= $this->load->view('backend/clients/form_head',null,true);
		$data['head']      .= $this->load->view('backend/clients/table_head',null,true);
		$data['head']      .= $this->load->view('backend/options/head',null,true);
		$data['user_id']    = $this->tank_auth->get_user_id();
		$data['username']   = $this->tank_auth->get_username();
		$data['profile_id'] = $this->tank_auth->get_profile_id();
		$data['clients']    = $this->clientmodel->getAllByOwnerId($data['user_id']);
		$data['invoices']   = $this->invoicemodel->getAllByOwnerId($data['user_id']);
		$data['profiles']   = $this->tank_auth->get_profiles();
		$this->load->view('backend/main', $data);
	}
}

